/**
	Aralork (Arale Framework)
	@author Random
	@version 0.01
**/

typeof($Overall)=="undefined" && ($Overall={});
$Overall.ErrorCode={
	"10001":"Class name must be uppercase first letter",
	"10002":"Name space must be lowercase first letter",
	"10003":"The import of the path is invalid"
};
var $NS={},
	$Clz={},
	
	$package=function(ns,clzName,clz){
		var defineNamespace=function(root,ns){
			ns=ns || "";
			ns=ns.replace(/\s/g,"");
			
			if(ns.length==0){
				return root;
			}else if(ns.indexOf(".")==-1){
				var nsc=ns.substr(0,1);
				if(nsc!=nsc.toLowerCase()){
					throw new Error("Error \""+ns+"\":"+$Overall.ErrorCode["10002"]);
				}
				typeof(root[ns])!="object" && (root[ns]={});
				return root[ns];
			}else{
				var _ns=ns.split(".")[0];
				typeof(root[_ns])!="object" && (root[_ns]={});
				return defineNamespace(root[_ns],ns.replace(/[^\.]*\./,""));
			}
		},
		
		c=clzName.substr(0,1);
		
		if(c!=c.toUpperCase()){
			throw new Error("Error \""+clzName+"\":"+$Overall.ErrorCode["10001"]);
		}
		
		defineNamespace($NS,ns)[clzName]=clz;
	},
	
	$import=function(ns,nsObj){
		var p=/([^\.]*[\.|a-z])/,
			nsp,
			nss;
		
		if (ns.replace(/\s/) == "") {
			throw new Error("Error $import:" + $Overall.ErrorCode["10003"]);
		}
		
		nsObj=nsObj || $NS;
		ns.search(p);
		nsp = RegExp.$1.replace(/\./,"");
		nss=ns.replace(p,"");
		if(nss.replace(/\s/) == "" || nss.substr(0,1)==nss.substr(0,1).toUpperCase()){
			$Clz=nsObj[nsp];
		}else{
			nsObj=nsObj[nsp];
			if(!nsObj){
				throw new Error("Error $import \""+nsp+"\":" + $Overall.ErrorCode["10003"]);
			}
			$import(nss,nsObj);
		}
	};
	
	var _ua = navigator.userAgent.toLowerCase();
	$IE = /msie/.test(_ua);
	$IE5 = /msie 5 /.test(_ua);
	$IE55 = /msie 5.5/.test(_ua);
	$IE6 = /msie 6/.test(_ua);
	$IE7 = /msie 7/.test(_ua);
	$IE8 = /msie 8/.test(_ua);
	$OPERA = /opera/.test(_ua);
	$MOZ = /gecko/.test(_ua);
	$SAFARI = /safari/.test(_ua);
	$XP=/windows nt 5.1/.test(_ua);
	$Vista=/windows nt 6.0/.test(_ua);
	$FF2=/Firefox\/2/i.test(_ua);
	$FF = /firefox/i.test(_ua);
	$CHROME = /chrome/i.test(_ua);
	$TT=/tencenttraveler/.test(_ua);
	$360=/360se/.test(_ua);
	$Maxthon=false;
	try{
		var t=window.external;
		_s.$Maxthon=t.max_version?true:false;
	}catch(e){}
	
	
	
	
	var $E=function(id){
		return document.getElementById(id);
	};
	var $N=function(name){
		return document.getElementsByName(name);
	};
	var $C=function(tag){
		return document.createElement(tag);
	};





Function.prototype.define=function(methods){
	this.prototype=methods;
	return this;
};

$package("functions","Style",
	{
		setOpacity:function(obj,v){
			v=isNaN(v)?0:v;
			v=Math.max(Math.min(1,v),0);
			if ($IE) {
				obj.style.filter = "alpha(opacity=" + v * 100 + ")";
			}
			else {
				obj.style.MozOpacity = v;
			}
			
		}
	}
);

$package("ui.tween","Transition",
	{
		simple:function(t,b,c,d){
			return c*t/d+b;
		},
		backEaseIn: function(t, b, c, d){
			var s = 1.70158;
			return c * (t /= d) * t * ((s + 1) * t - s) + b;
			
		},
		backEaseOut: function(t, b, c, d){
			var s = 1.70158;
			return c * ((t = t / d - 1) * t * ((s + 1) * t + s) + 1) + b;
		},
		backEaseInOut: function(t, b, c, d){
			var s = 1.70158;
			if ((t /= d / 2) < 1) {
				return c / 2 * (t * t * (((s *= (1.525)) + 1) * t - s)) + b;
			}
			return c / 2 * ((t -= 2) * t * (((s *= (1.525)) + 1) * t + s) + 2) + b;
		},
		bounceEaseOut: function(t, b, c, d){
			if ((t /= d) < (1 / 2.75)) {
				return c * (7.5625 * t * t) + b;
			}
			else 
				if (t < (2 / 2.75)) {
					return c * (7.5625 * (t -= (1.5 / 2.75)) * t + 0.75) + b;
				}
				else 
					if (t < (2.5 / 2.75)) {
						return c * (7.5625 * (t -= (2.25 / 2.75)) * t + 0.9375) + b;
					}
					else {
						return c * (7.5625 * (t -= (2.625 / 2.75)) * t + 0.984375) + b;
					}
		},
		bounceEaseIn: function(t, b, c, d){
			return c - Transition.bounceEaseOut(d - t, 0, c, d) + b;
		},
		bounceEaseInOut: function(t, b, c, d){
			if (t < d / 2) {
				return Transition.bounceEaseIn(t * 2, 0, c, d) * 0.5 + b;
			}
			else {
				return Transition.bounceEaseOut(t * 2 - d, 0, c, d) * 0.5 + c * 0.5 + b;
			}
			
		},
		regularEaseIn: function(t, b, c, d){
			return c * (t /= d) * t + b;
		},
		regularEaseOut: function(t, b, c, d){
			return -c * (t /= d) * (t - 2) + b;
		},
		regularEaseInOut: function(t, b, c, d){
			if ((t /= d / 2) < 1) {
				return c / 2 * t * t + b;
			}
			return -c / 2 * ((--t) * (t - 2) - 1) + b;
		},
		strongEaseIn: function(t, b, c, d){
			return c * (t /= d) * t * t * t * t + b;
		},
		strongEaseOut: function(t, b, c, d){
			return c * ((t = t / d - 1) * t * t * t * t + 1) + b;
		},
		strongEaseInOut: function(t, b, c, d){
			if ((t /= d / 2) < 1) {
				return c / 2 * t * t * t * t * t + b;
			}
			return c / 2 * ((t -= 2) * t * t * t * t + 2) + b;
		},
		elasticEaseIn: function(t, b, c, d){
			var a,p;
			if (t == 0) {
				return b;
			}
			if ((t /= d) == 1) {
				return b + c;
			}
		
			p = d * 0.3;
			
			if (!a || a < Math.abs(c)) {
				a = c;
				var s = p / 4;
			}
			else {
				var s = p / (2 * Math.PI) * Math.asin(c / a);
			}
			return -(a * Math.pow(2, 10 * (t -= 1)) * Math.sin((t * d - s) * (2 * Math.PI) / p)) + b;
		},
		elasticEaseOut: function(t, b, c, d){
			var a,p;
			if (t == 0) {
				return b;
			}
			if ((t /= d) == 1) {
				return b + c;
			}
			if (!p) {
				p = d * 0.3;
			}
			if (!a || a < Math.abs(c)) {
				a = c;
				var s = p / 4;
			}
			else {
				var s = p / (2 * Math.PI) * Math.asin(c / a);
			}
			return (a * Math.pow(2, -10 * t) * Math.sin((t * d - s) * (2 * Math.PI) / p) + c + b);
			
		},
		elasticEaseInOut: function(t, b, c, d){
			var a,p;
			if (t == 0) {
				return b;
			}
			if ((t /= d / 2) == 2) {
				return b + c;
			}
			if (!p) {
				var p = d * (0.3 * 1.5);
			}
			if (!a || a < Math.abs(c)) {
				var a = c;
				var s = p / 4;
			}
			else {
				var s = p / (2 * Math.PI) * Math.asin(c / a);
			}
			if (t < 1) {
				return -0.5 * (a * Math.pow(2, 10 * (t -= 1)) * Math.sin((t * d - s) * (2 * Math.PI) / p)) + b;
			}
			return a * Math.pow(2, -10 * (t -= 1)) * Math.sin((t * d - s) * (2 * Math.PI) / p) * 0.5 + c + b;
		}
	}
);
$import("ui.tween.Trasition");

$package("ui.tween","TweenStrategy",
	function(startValue,endValue,duration,motion){
		this.startValue=startValue || 0;
		this.endValue=endValue || 0;
		this.duration=duration || 0;
		this.motion=motion || $NS.ui.tween.Transition.simple;
	}.define({
		motion:null,
		duration:0,
		startValue:0,
		endValue:0,
		_itvID:0,
		_isTweenning:false,
		start:function(){
			if(this._isTweenning){
				return;
			}
			this._isTweenning=true;
			
			var me=this,
				t,
				sv=this.startValue,
				ev=this.endValue,
				d=this.duration,
				startTime=(new Date()).getTime();
				
			this._itvID=window.setInterval(function(){
				t=((new Date()).getTime()-startTime)/1000;
				t>me.duration && (t=me.duration);
				me.onTween && me.onTween(me.motion(t,sv,ev-sv,d));
				t==me.duration && me.stop();
			},25);
		},
		stop:function(){
			window.clearInterval(this._itvID);
			this._isTweenning=false;
			this.onEnd && this.onEnd();
		},
		onTween:function(value){},
		onEnd:function(){}
	})
);

$package("net.ajax","AjaxCreator",
	function(){
	}.define({
		create:function(){
			var _xhr;
			try{
				                   
				if (typeof(XMLHttpRequest)!="undefined") {
					_xhr = new XMLHttpRequest();                                               
				}else if (typeof(ActiveXObject)!="undefined") {
					_xhr = new ActiveXObject("Microsoft.XMLHTTP");
				}
  			}catch(e){}
			
			return _xhr;
		}
	})
);


$import("net.ajax.AjaxCreator");

$package("net.ajax","Ajax",
	function(){
		var me=this;
		me.$={
			_xrh:null,
			
			_opts:{
				url:"",
				method:"get",
				param:{}
			},
			
			_formatOpts:function(opts){
				opts.url && (this._opts.url=opts.url);
				opts.method && (this._opts.method=opts.method);
				opts.param && (this._opts.param=opts.param);
			},
			
			_initXRH:function(){
				this._xrh=(new $Clz.AjaxCreator()).create();
				this._xrh.onreadystatechange=function(){
					switch(me.$._xrh.readyState){
						case 4:
							if(me.$._xrh.status == 200) {
								me.onComplete && me.onComplete(me.$._xrh.responseText);
							}
							break;
					}
				};
			},
			
			_formatParam:function(p){
				var k,
					s=[];
				for(k in p){
					s.push([k,"=",p[k]].join(""));
				}
				return s.join("&");
			}
		};
		
		me.$._initXRH();
		
	}.define({
		request:function(opts){
			var xrh=this.$._xrh,
				op,
				opp,
				mth;
				
			this.$._formatOpts(opts);
			op=this.$._opts;
			opp=this.$._formatParam(op.param);
			mth=op.method.toUpperCase();
			mth=="GET" && (op.url.indexOf("?")!=-1?op.url+=opp:op.url+="?"+opp);
			
			xrh.open(this.$._opts.method.toUpperCase(),op.url+"&t="+(new Date()).getTime(),true);
			mth=="GET"?xrh.send(opp):xrh.send(null);
		},
		
		onComplete:function(){},
		onError:function(){}
	})
);

//************************************************Application******************************************

$import("ui.tween.Trasition");
$import("ui.tween.TweenStrategy");
$import("function.Style");

$package("application","InsertPanel",
	function(panel){
		var me=this;
		
		this.entity=panel;
		this.tween=new $NS.ui.tween.TweenStrategy(0,0,0.5,$NS.ui.tween.Transition.strongEaseOut);
		this.tween.onTween=function(value){
			$NS.functions.Style.setOpacity(me.entity,value);
		};
		$NS.functions.Style.setOpacity(me.entity,0);
		
	}.define({
		isTweening:false,
		entity:null,
		tween:null,
		show:function(){
			var me=this;
			this.tween.motion=$NS.ui.tween.Transition.strongEaseIn;
			this.entity.style.display="";
			this.tween.onEnd=function(){
				me.isTweening=false;
			};
			this._change(0,1);
		},
		hidden:function(){
			var me=this;
			this.tween.motion=$NS.ui.tween.Transition.strongEaseOut;
			this.tween.onEnd=function(){
				me.isTweening=false;
				me.entity.style.display="none";
			};
			this._change(1,0);
		},
		_change:function(s,e){
			if (!this.isTweening) {
				this.isTweening = true;
				this.tween.startValue = s;
				this.tween.endValue = e;
				this.tween.start();
			}
		}
	})
);




var $RunTime={
	
	showMoreJob:function(){
		$import("net.ajax.Ajax");
		var ajax=new $Clz.Ajax();
			
		ajax.onComplete=function(data){
			eval("data="+data);
			var template=[
				'<div class="Post" name="divcontent">',
					'<p>',
						'<span class="Time">#{date}</span>&nbsp;',
						'<span class="People">#{author}</span>&nbsp;',
						'发牢骚说：',
					'</p>',
					'<p>#{content}</p>',
				'</div>',
				'<div class="blank">&nbsp;</div>'
			].join(""),
			list=data.data,
			html=[];
	
			if (list.length > 0) {
				for (var i = 0, l = list.length; i < l; i++) {
					html.push(template.replace(/\#\{date\}/, list[i].date)
								.replace(/\#\{author\}/, list[i].author)
								.replace(/#\{content\}/, list[i].content));
				}
				
				document.getElementById("divList").innerHTML += html.join("");
				$Overall.more.start+=list.length;
			}
			
			
		};
		document.getElementById("MoreButton").onclick=function(){
			ajax.request({
				url:"/content",
				param:{
					start:$Overall.more.start,
					len:$Overall.more.len
				}
			});
		};
	},
	
	
	insertImgJob:function(){
		var divInsertImg=$E("divInsertImg"),
			txtInsertURL=$E("txtInsertURL"),
			btnShowInsertImg=$E("btnShowInsertImg"),
			btnInsertImg=$E("btnInsertImg"),
			btnCancelInsertImg=$E("btnCancelInsertImg"),
			getRBValue=function(name){
				var rbs=$N(name);
				for(var i=0,l=rbs.length;i<l;i++){
					if(rbs[i].checked){
						return rbs[i].value;
					}
				}
				return "";
			},
			insert=function(){
				var iv=getRBValue("insertType"),
					lv=getRBValue("alignType"),
					insertStr=["[",iv,"]",txtInsertURL.value,"[/",iv,"]"].join("");
				$E("content").value+=insertStr;
			};
		
		var insertPanel=new $NS.application.InsertPanel(divInsertImg);
		btnShowInsertImg.onclick=function(){
			insertPanel.show();
		};
		btnCancelInsertImg.onclick=function(){
			insertPanel.hidden();
		};
		btnInsertImg.onclick=function(){
			insertPanel.hidden();
			insert();
		};
	}

	
};


/**
 * @author random
 */

window.onload=function(){
	var k;
	for(k in $RunTime){
		String.prototype.slice.call($RunTime[k],0,8)=="function" && $RunTime[k].call();
	}
	setName();
};
